﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;

namespace tank
{
    class _Bullet
    {
        public Point Location { get; set; }
        public Size Size { get; set; }
        public _Orientation Or { get; set; }
        public int Velocity { get; set; }
        public bool IsRunning { get; set; }
        public Image Image { get; set; }
        public bool IsEnemy { get; set; }

        public Rectangle Rectangle
        {
            get { return new Rectangle(Location, Size); }
        }

        int maxWidth, maxHeight;

        public _Bullet(int maxWidth, int maxHeight, _Orientation Or)
        {
            this.Or = Or;
            Location = new Point(-10, -10);
            Size = new Size(6, 6);
            Velocity = 8;
            Image = tank.Properties.Resources.bulletImage;

            this.maxWidth = maxWidth;
            this.maxHeight = maxHeight;
            switch (Or)
            {
                case _Orientation.Up: break;
                case _Orientation.Down: Image.RotateFlip(RotateFlipType.Rotate180FlipNone); break;
                case _Orientation.Left: Image.RotateFlip(RotateFlipType.Rotate270FlipNone); break;
                case _Orientation.Right: Image.RotateFlip(RotateFlipType.Rotate90FlipNone); break;
            }
        }

        public void Reset()
        {
            Location = new Point(-10, -10);
            IsRunning = false;
        }

        public void StartShoot(Point location, Size size, _Orientation orientation, bool IsEnemy)
        {
            this.IsEnemy = IsEnemy;
            switch (orientation)
            {
                case _Orientation.Up:
                    {
                        Location = new Point(location.X + size.Width / 2 - Size.Width / 2, location.Y - Size.Height / 2);
                        break;
                    }

                case _Orientation.Down:
                    {
                        Location = new Point(location.X + size.Width / 2 - Size.Width / 2, location.Y + size.Height - Size.Height / 2);
                        break;
                    }

                case _Orientation.Left:
                    {
                        Location = new Point(location.X - Size.Width / 2, location.Y + size.Height / 2 - Size.Height / 2);
                        break;
                    }

                case _Orientation.Right:
                    {
                        Location = new Point(location.X + size.Width - Size.Width / 2, location.Y + size.Height / 2 - Size.Height / 2);
                        break;
                    }
            }
            IsRunning = true;
            Or = orientation;
        }

        public void Update(ref List<_Block> blocks)
        {
            int dx = 0, dy = 0;
            if (IsRunning)
            {
                if (Or == _Orientation.Up)
                    dy = -Velocity;
                else
                    if (Or == _Orientation.Down)
                        dy = Velocity;
                    else
                        if (Or == _Orientation.Left)
                            dx = -Velocity;
                        else
                            if (Or == _Orientation.Right)
                                dx = Velocity;

                Rectangle rect = new Rectangle(Location, Size);
                rect.Offset(dx, dy);

                bool intersect = false;

                for (int i = 0; i < blocks.Count; i++)
                {
                    switch (blocks[i].BlockType.ToString())
                    {
                        case "Brick_Box_Def":
                            if (RectIntersect(rect, new Rectangle(blocks[i].Location, blocks[i].Size)))
                            {
                                intersect = true;
                                blocks.RemoveAt(i);
                                i--;
                            }
                            break;
                        case "Brick_Box_Red":
                            if (RectIntersect(rect, new Rectangle(blocks[i].Location, blocks[i].Size)))
                            {
                                blocks[i].life--;
                                //blocks[i].Img = tank.Properties.Resources._1_9_1;
                                if (blocks[i].Var == 1) blocks[i].Img = tank.Properties.Resources._1_8_1;
                                else blocks[i].Img = tank.Properties.Resources._1_9_1;
                                if (blocks[i].life < 1) { blocks.RemoveAt(i); i--; }
                                intersect = true;
                            }
                            break;
                        case "Brick_Box_Iron": 
                            if (RectIntersect(rect, new Rectangle(blocks[i].Location, blocks[i].Size)))
                                intersect = true;
                            break;
                        case "Place_Green": break;
                        case "Base":
                            if (RectIntersect(rect, new Rectangle(blocks[i].Location, blocks[i].Size)))
                            {
                                intersect = true;
                                if (IsEnemy == true)
                                    blocks[i].life--;
                            }
                            break;
                    }

                    if (rect.Top > 0 && rect.Bottom < maxHeight && rect.Left > 0 && rect.Right < maxWidth && !intersect)
                        Location = rect.Location;
                    else
                        Reset();
                }
            }
        }

        public void Draw(Graphics graphics)
        {
            graphics.DrawImage(Image, Location);
        }


        bool RectIntersect(Rectangle rect1, Rectangle rect2)
        {
            bool intersect = false;

            if (rect1.Left >= rect2.Left && rect1.Left <= rect2.Right && rect1.Top >= rect2.Top && rect1.Top <= rect2.Bottom
               || rect1.Left >= rect2.Left && rect1.Left <= rect2.Right && rect1.Bottom >= rect2.Top && rect1.Bottom <= rect2.Bottom
               || rect1.Right >= rect2.Left && rect1.Right <= rect2.Right && rect1.Top >= rect2.Top && rect1.Top <= rect2.Bottom
               || rect1.Right >= rect2.Left && rect1.Right <= rect2.Right && rect1.Bottom >= rect2.Top && rect1.Bottom <= rect2.Bottom)
                intersect = true;

            return intersect;
        }
    }
}
